<?php namespace App\Http\Controllers\Api;

use App\ActivePhoto;
use App\ActivePhotoImg;
use App\ActiveVotes;
use App\Http\Controllers\Controller;
use App\Http\Services\FileManager;
use App\Http\Services\UserManager;
use App\User;
use Illuminate\Http\Request;

class ActiveController extends Controller
{
    public function getPhotoList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        if(!$user){
            return response()->json(['level'=>'error','message'=>'请先登录！','data'=>'no_login']);
        }

        $thatOnePhotos = ActivePhoto::whereBetween("number",[1,9])->get();

        if(count($thatOnePhotos) > 0){
            foreach ($thatOnePhotos as $thatOnePhoto){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($thatOnePhoto->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $thatOnePhoto->img_list = $imgList;
            }
        }

        $thatTwoPhotos = ActivePhoto::whereBetween("number",[10,18])->get();

        if(count($thatTwoPhotos) > 0){
            foreach ($thatTwoPhotos as $thatTwoPhoto){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($thatTwoPhoto->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $thatTwoPhoto->img_list = $imgList;
            }
        }

        $thatThreePhotos = ActivePhoto::whereBetween("number",[19,27])->get();

        if(count($thatThreePhotos) > 0){
            foreach ($thatThreePhotos as $thatThreePhoto){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($thatThreePhoto->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $thatThreePhoto->img_list = $imgList;
            }
        }

        $thatFourPhotos = ActivePhoto::whereBetween("number",[28,30])->get();

        if(count($thatFourPhotos) > 0){
            foreach ($thatFourPhotos as $thatFourPhoto){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($thatFourPhoto->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $thatFourPhoto->img_list = $imgList;
            }
        }

        $result = [
            ["num"=>"01-09","list"=>$thatOnePhotos],
            ["num"=>"10-18","list"=>$thatTwoPhotos],
            ["num"=>"19-27","list"=>$thatThreePhotos],
            ["num"=>"28-30","list"=>$thatFourPhotos],
        ];

        $phone = "";
        if(!is_null($user->phone))$phone = $user->phone;

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result,"phone"=>$phone]);
    }

    public function setVote(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        if(!$user){
            return response()->json(['level'=>'error','message'=>'请先登录！','data'=>'no_login']);
        }

        $ids = $request->get("ids");
        $phone = $request->get("phone");

        if(!is_null($phone) && trim($phone) != ""){
            $user->phone = $phone;
            $user->save();
        }



        //获取当天的最大时间和最小时间
        $ty = date('Y', time());
        $tm = date('m', time());
        $td = date('d', time());
        $todayStartTime = mktime(0,0,0,$tm,$td,$ty);
        $todayEndTime   = mktime(23,59,59,$tm,$td,$ty);

        $userVoteToday = ActiveVotes::whereUserId($user->id)->whereBetween("created_at",[date('Y-m-d H:i:s',$todayStartTime),date('Y-m-d H:i:s',$todayEndTime)])->first();
        if($userVoteToday){
            return response()->json(["level"=>"error","message"=>"您今天已经投过票了哦！"]);
        }

        if(count($ids) > 5){
            return response()->json(["level"=>"error","message"=>"每天最多只能投5票哦！"]);
        }

        if(count($ids) > 0){
            foreach ($ids as $id){
                $thatActive = ActivePhoto::whereId($id)->first();
                $hasVote = ActiveVotes::whereUserId($user->id)->whereActiveId($id)->whereBetween("created_at",[date('Y-m-d H:i:s',$todayStartTime),date('Y-m-d H:i:s',$todayEndTime)])->first();
                if(!$hasVote && $thatActive){
                    $newVote = new ActiveVotes();
                    $newVote->active_id = $id;
                    $newVote->user_id = $user->id;
                    $newVote->vote_num = 1;
                    $newVote->save();

                    $thatActive->vote_num = $thatActive->vote_num + 1;
                    $thatActive->save();
                }
            }
        }

        return response()->json(["level"=>"success","message"=>"投票成功，感谢您的支持！"]);
    }

    public function getRank(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        if(!$user){
            return response()->json(['level'=>'error','message'=>'请先登录！','data'=>'no_login']);
        }

        $topThrees = ActivePhoto::orderBy("vote_vm","desc")->limit(3)->get();

        if(count($topThrees) > 0){
            foreach ($topThrees as $topThree){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($topThree->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $topThree->img_list = $imgList;

                $topThree->vote_num = $topThree->vote_num + $topThree->vote_vm;
            }
        }

        $lastVotes = ActivePhoto::orderBy("vote_vm","desc")->skip(3)->take(27)->get();

        if(count($lastVotes) > 0){
            foreach ($lastVotes as $lastVote){
                $thatOnePhotoImgs = ActivePhotoImg::whereActivePhotoId($lastVote->id)->get();

                $imgList = [];
                if(count($thatOnePhotoImgs) > 0){
                    foreach ($thatOnePhotoImgs as $thatOnePhotoImg){
                        $imgUrl = FileManager::getImgFromOssUrl($thatOnePhotoImg->img_id);
                        array_push($imgList,$imgUrl);
                    }
                }

                $lastVote->img_list = $imgList;

                $lastVote->vote_num = $lastVote->vote_num + $lastVote->vote_vm;
            }
        }

        $result = [
            "top_three"=>$topThrees,
            "last"=>$lastVotes
        ];

        return response()->json(["level"=>"success","message"=>"获取成功","data"=>$result]);
    }

    public function setData(Request $request){
        $allVotes = ActivePhoto::all();

        if(count($allVotes) > 0){
            foreach ($allVotes as $allVote){
                $allVote->vote_vm =  (int)($allVote->vote_num / 1300 * 80000) + rand(1000,10000);
                $allVote->save();
            }
        }

        echo "complete";
    }
}